package com.ymatou.query.trading.api.service;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.common.enums.PlatformEnum;
import com.ymttest.utils.EnvSetup;

/**
 * 获取不同平台上面的订单数量Call FreeMaker Template自动生成代码
 */
public class GetOrderCountOfPlatformCall extends YmatouBaseCall {
	private static String opurl = "api/BuyerQuery/GetOrderCountOfPlatform";

	public GetOrderCountOfPlatformCall() {
		super(EnvSetup.getSystemUrl("api_order_query"), opurl, "GET", "PARAM");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept", "text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	/**
	 * 获取Code
	 * 
	 * @return
	 */
	public String getCode() {
		String Code = null;
		try {
			Code = new JSONObject(getReturnData()).getString("Code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Code;
	}

	/**
	 * 获取BCode
	 * 
	 * @return
	 */
	public String getBCode() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("BCode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Msg
	 * 
	 * @return
	 */
	public String getMsg() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("Msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Data
	 * 
	 * @return
	 */
	public JSONObject getData() {
		JSONObject Data = null;
		try {
			Data = new JSONObject(getReturnData()).getJSONObject("Data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

	/**
	 * 获取OrderCountOfPlatformDic
	 * 
	 * @return
	 */
	public JSONObject getOrderCountOfPlatformDic() {
		JSONObject OrderCountOfPlatformDic = null;
		try {
			OrderCountOfPlatformDic = getData().getJSONObject("OrderCountOfPlatformDic");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return OrderCountOfPlatformDic;
	}

	/**
	 * 获取指定平台类型订单数量
	 * 
	 * @param Ordertype
	 * @return
	 */
	public HashMap<PlatformEnum, Integer> getOrderNum() {
		int num = 0;
		HashMap<PlatformEnum, Integer> map = new HashMap<PlatformEnum, Integer>();
		for (PlatformEnum OrderType : PlatformEnum.values()) {
			String type = String.valueOf(OrderType);
			try {
				if (!(type.equals("Unknown"))) {
					num = getOrderCountOfPlatformDic().getInt(String.valueOf(OrderType));
				}
				// Logger.debug(num);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			map.put(OrderType, num);
		}
		return map;
	}
}
